package com.industrial.premu.controller.chart;

import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.service.PreMuEnergyConsumptionIndexAnalysisService;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 能耗指标
 */
@RestController
@RequestMapping("/ecia")
public class PreMuEnergyConsumptionIndexAnalysisController extends BaseController {

    @Autowired
    private PreMuEnergyConsumptionIndexAnalysisService preMuEnergyConsumptionIndexAnalysisService;


    /**
     * 查询近五年的数据
     * @param preChartsDto
     * @return
     */
    @GetMapping("/jwnsj")
    public AjaxResult queryDataFromThePastFiveYears(PreMuChartsDto preChartsDto){
        int years=Integer.valueOf(preChartsDto.getYears());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }else if(roles.contains("admin")){

                preChartsDto.setProvince("370000");
            }
        }


        if(years>=2019){
            preChartsDto.setEndYears(String.valueOf(years));
            int  startYears=years-4;
            if(startYears>2019){
                preChartsDto.setStartYears(String.valueOf(startYears));
            }else{
                preChartsDto.setStartYears("2019");
            }
            preChartsDto.setYears("");
        }

        List<PreMuChartsVo> chartsVoList= new ArrayList<>();

        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
            List<PreMuChartsVo> cList = preMuEnergyConsumptionIndexAnalysisService.groupEconomyByCityAndCounty(preChartsDto);
            List<PreMuChartsVo> pList=  preMuEnergyConsumptionIndexAnalysisService.groupEconomyByProvince(preChartsDto);

            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < cList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(cList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(cList.get(i).getCIndexNum());
                    }
                }
            }
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
            List<PreMuChartsVo> cList = preMuEnergyConsumptionIndexAnalysisService.groupEconomyByCityAndCounty(preChartsDto);
            List<PreMuChartsVo> pList=  preMuEnergyConsumptionIndexAnalysisService.groupEconomyByProvince(preChartsDto);

            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < cList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(cList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(cList.get(i).getCIndexNum());
                    }
                }
            }
        }else if(roles.contains("p_01_011")){
            preChartsDto.setCity("370000");
            List<PreMuChartsVo> pList=  preMuEnergyConsumptionIndexAnalysisService.groupEconomyByProvince(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
        }else if(roles.contains("admin")){
            preChartsDto.setCity("370000");
            List<PreMuChartsVo> pList=  preMuEnergyConsumptionIndexAnalysisService.groupEconomyByProvince(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
        }

//        List<PreMuChartsVo> chartsVoList= preMuEnergyConsumptionIndexAnalysisService.groupEconomyByYears(preChartsDto);
        return AjaxResult.success(chartsVoList);
    }


    /**
     * 区域表格
     * @param preChartsDto
     * @return
     */
    @GetMapping("/queryAreaTable")
    public TableDataInfo queryAreaTable(PreMuChartsDto preChartsDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }else if(roles.contains("admin")){

                preChartsDto.setProvince("370000");
            }
        }
        startPage();
        List<PreMuBenefitEvaluation> r= preMuEnergyConsumptionIndexAnalysisService.queryAreaTable(preChartsDto);
        return getDataTable(r);
    }

    /**
     * 查询图表
     * @param preChartsDto
     * @return
     */
    @GetMapping("/queryAreaEcharts")
    public AjaxResult queryAreaEcharts(PreMuChartsDto preChartsDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }else if(roles.contains("admin")){
                preChartsDto.setProvince("370000");
            }
        }

        return AjaxResult.success(preMuEnergyConsumptionIndexAnalysisService.queryAreaEcharts(preChartsDto));
    }

    /**
     * 查询行业表格
     * @param preChartsDto
     * @return
     */
    @GetMapping("/queryHyTable")
    public TableDataInfo queryHyTable(PreMuChartsDto preChartsDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }
        }
        startPage();
        List<PreMuBenefitEvaluation> r= preMuEnergyConsumptionIndexAnalysisService.queryHyTable(preChartsDto);
        return getDataTable(r);
    }

    /**
     * 查询行业图表
     * @param preChartsDto
     * @return
     */
    @GetMapping("/queryHyEcharts")
    public AjaxResult queryHyEcharts(PreMuChartsDto preChartsDto){

        preChartsDto.setPageNum(0);
        preChartsDto.setPageSize(9);
        preChartsDto.setPageFlag("1");

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }
        }

        return AjaxResult.success(preMuEnergyConsumptionIndexAnalysisService.queryHyEcharts(preChartsDto));
    }
}
